﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class UIGray : MonoBehaviour
{
    class ImageInfo
    {
        public Image image;
        public Color color;
    }

    List<ImageInfo> m_images = new List<ImageInfo>();

	void Awake()
	{
		//enabled = false;
	}

    void OnEnable()
    {
		SetGray ();
    }

	public void SetGray()
	{
		m_images.Clear ();
		ImageInfo info = null;
		Image[] images = gameObject.GetComponentsInChildren<Image> ();
		for (int i = 0; i < images.Length; ++i) {
			if(!images[i].gameObject.activeSelf)	//如果当前对象是未激活状态就没有必要Gray了
			{
				continue;
			}
			info = new ImageInfo ();
			info.image = images [i];
			info.color = info.image.color;

			m_images.Add (info);

			Color color = info.image.color;
			color.r = color.g = color.b = 0;
			info.image.color = color;
		}
	}

	public void SetNormal()
	{
		if (m_images == null)
			return;

		ImageInfo info = null;
		for (int i = 0; i < m_images.Count; ++i) {
			info = m_images [i];
			if(info != null && info.image != null)
				info.image.color = info.color;
		}
		
		m_images.Clear ();
	}

    void OnDisable()
    {
		SetNormal ();
    }
}